package com.rao.travel.controller;

import com.rao.travel.pojo.SysLog;
import com.rao.travel.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author Srao
 * @version 1.0
 * @className LogAop
 * @date 2019/11/5 21:30
 * @package com.rao.travel.controller
 * @description 通知处理类
 */
@Component
@Aspect
public class LogAop {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SysLogService sysLogService;

    private Date visitTime;//访问时间
    private Class clazz;//访问的类
    private Method method;//访问的方法

    //前置通知
    @Before("execution(* com.rao.travel.controller..*(..))")
    public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException {
        visitTime = new Date();//获取访问时间
        clazz = joinPoint.getTarget().getClass();//获取访问的类
        String methodName = joinPoint.getSignature().getName();//获取访问的方法名
        Object[] args = joinPoint.getArgs();//获取方法的参数

        if (args == null || args.length == 0){
            method = clazz.getMethod(methodName);//获取无参数的方法
        }else {
            Class[] clazzArgs = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                clazzArgs[i] = args[i].getClass();
            }
            method = clazz.getMethod(methodName, clazzArgs);//获取有参的方法
        }

    }

    //后置通知
    @After("execution(* com.rao.travel.controller..*(..))")
    public void doAfter(JoinPoint joinPoint){
        //获取访问的时长
        Long executionTime = new Date().getTime() - visitTime.getTime();

        //获取访问的url
        String url = "";
        if (clazz!=null && method!=null && clazz!=LogAop.class){
            /*获得类上的RequestMapping注解*/
            RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (classAnnotation != null){
                String[] classValue = classAnnotation.value();

                /*获取方法上的RequestMapping注解*/
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation != null){
                    String[] methodValue = methodAnnotation.value();

                    url = classValue[0] + methodValue[0];


                    /*获取访问者的IP*/
                    String ip = request.getRemoteAddr();

                    /*获取访问者的用户名*/
                    SecurityContext context = SecurityContextHolder.getContext();
                    User user = (User) context.getAuthentication().getPrincipal();
                    String username = user.getUsername();


                    /*把信息封装到syslog*/
                    SysLog sysLog = new SysLog();
                    sysLog.setExecutionTime(executionTime);
                    sysLog.setIp(ip);
                    sysLog.setMethod("[类名] " + clazz.getName() + ":[方法名] " + method.getName());
                    sysLog.setUrl(url);
                    sysLog.setUsername(username);
                    sysLog.setVisitTime(visitTime);

                    sysLogService.save(sysLog);
                }
            }
        }

    }
}
